using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class QuestUI : singleTon<QuestUI>
{
    [Header("Element")]
    public GameObject questPanel;
    public ItemToolTip toolTip;
    private bool isOpen;

    [Header("Quest Name")] 
    public RectTransform questListTransform;

    public QuestNameButton questNameButton;

    [Header("Text Content")] 
    public Text questContentText;

    [Header("Requirement")] 
    public RectTransform requirementTransform;

    public QuestRequirment requirement;
    
    [Header("Reward")]
    public RectTransform rewardTransform;

    public ItemUI rewardSlot;

    protected override void Awake()
    {
        base.Awake();
        DontDestroyOnLoad(this.gameObject);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q)&&Input.GetKey(KeyCode.LeftAlt))
        {
            isOpen = !isOpen;
            questPanel.SetActive(isOpen);
            toolTip.gameObject.SetActive(false);
            questContentText.text = "";
            SetupQuestList();
        }
    }

    public void  SetupQuestList()
    {
        foreach (Transform item in questListTransform)
        {
            Destroy(item.gameObject);
        }

        foreach (Transform item in rewardTransform)
        {
            Destroy(item.gameObject);
        }

        foreach (Transform item in requirementTransform)
        {
            Destroy(item.gameObject);
        }

        foreach (var task in QuestManager.Instance.QuestTasks)
        {
            var newTask = Instantiate(questNameButton, questListTransform);
            newTask.SetupQuestName(task.QuestData);
            newTask.questContentText = questContentText;
        }
    }

    public void SetupRequirmentList(QuestData_SO questData)
    {
        foreach (Transform item in requirementTransform)
        {
            Destroy(item.gameObject);
        }

        foreach (var require in questData.questRequires)
        {
            var quest = Instantiate(requirement, requirementTransform);
            if (questData.isFinished)
            {
                quest.SetupRequirement(require.requiretName,true);
            }
            else 
                quest.SetupRequirement(require.requiretName,require.requireAmount,require.currentAmount);
        }
    }

    public void SetupRewardItemList(ItemData_SO rewardItemdata,int amount)
    {
        var rewardItem = Instantiate(rewardSlot, rewardTransform);
        rewardItem.SetupItemUI(rewardItemdata,amount);
    }
    
    
}
